package com.hr.dao;

import com.hr.model.Position;
import com.hr.model.UW;
import com.hr.model.Wanted;
import org.apache.ibatis.annotations.*;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Repository
public interface WantedDAO
{
    @Results(id = "wanted", value = {
            @Result(id = true, property = "id", column = "w_id"),
            @Result(property = "name", column = "w_name"),
            @Result(property = "salaryDown", column = "w_salaryDown"),
            @Result(property = "salaryUP", column = "w_salaryUP"),
            @Result(property = "tel", column = "w_tel"),
            @Result(property = "testLink", column = "w_testLink"),
            @Result(property = "intro", column = "w_intro"),
            @Result(property = "enable", column = "w_enable"),

            @Result(property = "company", column = "c_id", one = @One(select = "com.hr.dao.CompanyDAO.getCompanyByCId")),
            @Result(property = "department", column = "dep_id", one = @One(select = "com.hr.dao.DepartmentDAO.getDepartmentByDepId")),
            @Result(property = "users", column = "u_id", many = @Many(select = "com.hr.dao.UserDAO.getUsersByWId")),
    })
    @Select("select * from wanted where w_id=#{wid}")
    Wanted getWantedByWId(@Param("wid") int wid);

    @ResultMap("wanted") @Select("select * from wanted ")
    List<Wanted> getAllWanteds();

    @ResultMap("wanted")
    @Select("select * from wanted join user_wanted uw on wanted.w_id = uw.w_id where u_id=#{uid}")
    List<Wanted> getWantedsByUId(@Param("uid") int uid);

    @ResultMap("wanted")
    @Select("select * from wanted  where c_id=#{cid}")
    List<Wanted> getWantedsByCId(@Param("cid") int cid);

    @SelectKey(keyProperty = "w.id",before = false,resultType = Integer.class,statement = "select last_insert_id()")
    @Insert("insert into wanted (dep_id, c_id, w_name, w_salaryDown, w_salaryUP, w_tel, w_TestLink, w_intro,w_enable) " +
            "values (#{w.department.id}, #{w.company.id}, #{w.name}, #{w.salaryDown}, #{w.salaryUP}, #{w.tel}, #{w.testLink}, #{w.intro},#{w.enable})")
    void insertWanted(@Param("w") Wanted w);

    @Select("select uw_status from user_wanted  where u_id=#{uid} and w_id=#{wid}")
    String getWantedStatusByU_W_Id(@Param("uid")int uid, @Param("wid")int wid);

    @Results(id = "uw", value = {
            @Result(property = "uid", column = "u_id"),
            @Result(property = "wid", column = "w_id"),
            @Result(property = "status", column = "uw_status"),
            @Result(property = "archive", column = "u_id", one = @One(select = "com.hr.dao.ArchiveDAO.getArchiveByUId")),
    })
    @Select("select * from  user_wanted where w_id=#{wid}")
    List<UW> getUWsByWId(@Param("wid")int wid);

    @ResultMap("uw")
    @Select("select * from  user_wanted where u_id=#{uid} and w_id=#{wid}")
    UW getUWByU_W_Id(@Param("uid")int uid,@Param("wid")int wid);


    @Insert("insert into user_wanted (u_id, w_id, uw_status) values (#{uid},#{wid},#{status})")
    void insetUW(@Param("uid")int uid,@Param("wid")int wid,@Param("status")String status);

    @Update("update user_wanted set uw_status=#{status} where u_id=#{uid} and w_id=#{wid}")
    void updateUW(@Param("uid")int uid,@Param("wid")int wid,@Param("status")String status);

    @Update("update wanted set w_enable=#{enable} where w_id=#{wid}")
    void checkWanted(@Param("wid")int wid,@Param("enable")boolean enable);

}